﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jobs.Inf.Protector;

namespace Jobs.Domain.Service
{
    public class EMail
    {
        /// <summary>
        /// 邮件主题
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// 发件箱
        /// </summary>
        public string From { get; set; }
        /// <summary>
        /// 收件箱地址
        /// </summary>
        public string Recipient { get; set; }
        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body { get; set; }
        /// <summary>
        /// 发件箱账号
        /// </summary>
        private string CredentialUserName {get;set;}
        /// <summary>
        /// 发件箱密码
        /// </summary>
        private string CredentialPassword { get; set; }

        public EMail()
        {
            this.From = "rogers1754@sina.com";
            this.CredentialUserName = "rogers1754";
            this.CredentialPassword = "19860904";
        }
        public void Send(string subject,string recipient,string body)
        {
            this.Subject = subject;
            this.Recipient = recipient;
            this.Body = body;

            System.Net.Mail.MailMessage myEmail = new System.Net.Mail.MailMessage();
            Encoding eEncod = Encoding.GetEncoding("utf-8");
            myEmail.From = new System.Net.Mail.MailAddress(this.From.Trim(),null, eEncod);
            myEmail.To.Add(this.Recipient.Trim());
            myEmail.Subject = this.Subject.Trim();
            myEmail.IsBodyHtml = true;
            myEmail.Body = this.Body.Trim();
            myEmail.Priority = System.Net.Mail.MailPriority.Normal;
            myEmail.BodyEncoding = Encoding.GetEncoding("utf-8");


            System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient();
            smtp.Host = "smtp." + myEmail.From.Host;
            smtp.Credentials = new System.Net.NetworkCredential(this.CredentialUserName, this.CredentialPassword);
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

            try
            {
                smtp.Send(myEmail);
            }
            catch (System.Net.Mail.SmtpException e)
            {
                throw new ContextException(String.Format("发送邮件至:{0}失败", recipient), ExceptionCode.OperationFaild);
            }
        }

        /// <summary>
        /// 构建注册激活邮件内容
        /// </summary>
        /// <param name="username"></param>
        /// <param name="token">注册时生成的token值</param>
        /// <returns></returns>
        public string BuildWelcomeEmailBody(string username, string fixtoken, DateTime tokenDate)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div style='line-height: 170%; font-size: 14px; margin-left: auto; margin-right: auto;width: 80%; overflow: hidden'>");
            sb.Append("<p>尊敬的用户：" + username + "，<br />您好！</p>");
            sb.Append("<p>欢迎您注册重庆大学生兼职网，请点击以下链接激活您的账号</p>");
            sb.Append(String.Format("<p><a href=\"{0}\">{0}</a></p>", "http://www.qem.com/Account/Activation/" + fixtoken));
            sb.Append("</div>");
            return sb.ToString();
        }


        public string BuildResetPasswordEmailBody(string username, string fixtoken, DateTime tokenDate)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div style='line-height: 170%; font-size: 14px; margin-left: auto; margin-right: auto;width: 80%; overflow: hidden'>");
            sb.Append("<p>尊敬的用户：" + username + "，<br />您好！</p>");
            sb.Append("<p>欢迎您注册重庆大学生兼职网</p>");
            sb.Append("<p>您收到这封邮件是因为您使用了重设密码功能，现请点击以下链接重设您的密码。</p>");
            sb.Append(String.Format("<p><a href=\"{0}\">{0}</a></p>", "http://www.qem.com/Account/ResetPwd/" + fixtoken));
            sb.Append("</div>");
            return sb.ToString();
        }
    }
}
